<?php

class AlipayController extends Controller
{
	/*
	//支付方法
	public function actionGotoPay()
	{
		$alipay = Yii::app()->alipay;
		// If starting a guaranteed payment, use AlipayGuaranteeRequest instead
		$request = new AlipayDirectRequest();
		$request->out_trade_no = "unique order number";
		$request->subject = "product name";
		$request->body = "product description";
		$request->total_fee = 100.00;
		// Set other optional params if needed
		$form = $alipay->buildForm($request);
		echo $form;
		exit();
	}
	*/
	
	// 服务器异步通知页面路径
	public function actionNotify() {

		$handle = fopen(Yii::app()->basePath.'../../alipay.txt', "wb");
		$info = empty($_POST)?$_POST:$_GET;
   		fwrite($handle,"<?php \n return array(\n\t'" . implode("',\n\t'", $info) . "'\n);\n?>");
   		fclose($handle);
   		//print_r($_GET);
   		//exit;	
   		
		$order_id = $_POST['out_trade_no'];//订单号
		$orders = $this->loadModel($order_id);
		$product = $orders->orderProduct['0']->attributes;//取订单产品	
		//实例化支付类
		$alipay = new $orders->payment->class_name();		
		//配置
		$alipay->pay_config = $payment->zpy_config;	
		if ($alipay->verifyNotify()) {
			$order_id = $_POST['out_trade_no'];//订单号
			$trade_no = $_POST['trade_no'];//订单号
			$order_fee = $_POST['total_fee'];
			if($_POST['trade_status'] == 'TRADE_FINISHED' || $_POST['trade_status'] == 'TRADE_SUCCESS') {
				$this->updateOrderStatus($order_id,$trade_no,2,null,5);
				OrdMsg::model()->setOrdMsg($product['zm_id'],$order_id,4);
				echo 'success';				
			}
			else {
				$this->updateOrderStatus($order_id,$trade_no,2,null,5);
				OrdMsg::model()->setOrdMsg($product['zm_id'],$order_id,4);	
				echo 'success';			
			}
		} else {
			echo "fail";			
            exit();
		}
	}
	
	//页面跳转同步通知页面路径
	public function actionReturn() {
		$order_id = $_GET['out_trade_no'];//订单号
		$orders = $this->loadModel($order_id);
		$product = $orders->orderProduct['0']->attributes;//取订单产品
		//实例化支付类
		$alipay = new $orders->payment->class_name();		
		//配置
		$alipay->pay_config = $orders->payment->zpy_config;		
		//print_r($alipay->verifyReturn());exit;
		if ($alipay->verifyReturn()) {
			//echo "fdddddddd";exit;
			$trade_no = $_GET['trade_no'];//交易号
			$total_fee = $_GET['total_fee'];	
			if($_GET['trade_status'] == 'TRADE_FINISHED' || $_GET['trade_status'] == 'TRADE_SUCCESS') {
				$this->updateOrderStatus($order_id,$trade_no,2);
				OrdMsg::model()->setOrdMsg($product['zm_id'],$order_id,2);
				header301('/member/index/orders/vieworder/orderid/'.$order_id);
			}
			else {
				$this->updateOrderStatus($order_id,$trade_no,2);
				OrdMsg::model()->setOrdMsg($product['zm_id'],$order_id,2);
				header301('/member/index/orders/vieworder/orderid/'.$order_id);
			}
		} else {
			//echo "fail";
			throw new CHttpException(404,'交易失败！');
            exit();
		}
	}



	/*
     * 更新订单支付状态
     * @param $order_id 订单ID
     * @param $payid 支付方式   
     * @param $paystatus 支付状态    
     */
    public function updateOrderStatus($orderid,$trade_no,$paystatus=null,$status=null,$expstatus=null)
    {
    	$model=$this->loadModel($orderid);
    	// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);
		$order['zo_id'] = $orderid ;		
		if($paystatus!=null){
		$order['zo_pay_status'] = $paystatus;
		$order['zo_trade_no'] = $trade_no;		
		$order['zo_pay_time'] = time();
		}
		if($status!=null){
		$order['zo_status'] = $status;
		}		
		if($expstatus!=null){
		$order['zo_exp_status'] = $expstatus;
		}		
		
		//添加收支明细
		$account = new  MemAccount();
		$account->zm_id = $model->zm_id_cmp;//会员ID
		$account->zmc_type = 1;//类型:1增加2.减少
		$account->zmc_event = 1;//'操作类型:1订单2充值3提现4现金券5退款
		$account->zmc_orderid = $orderid;//订单ID/流水号
		$account->zmc_amount = $model->zo_amount;//金额
		$account->zmc_note = "";//备注
		$account->save();
		
		if(isset($order))
		{
			$model->attributes = $order;
			if($model->save())
				return true;
		}
    }

    /**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=Order::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

}